package com.ruoyi.wxApp.service.impl;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.wxApp.common.wxconfig.DisWxuserConstants;
import com.ruoyi.wxApp.domain.DisWxuser;
import com.ruoyi.wxApp.login.util.HttpClientUtil;
import com.ruoyi.wxApp.mapper.DisWxuserMapperWxapp;
import com.ruoyi.wxApp.service.WxLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.net.UnknownHostException;
import java.util.Random;

/**
 * 微信用户Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Service
public class DisWxuserServiceImplWxapp implements WxLoginService {

    @Autowired
    private HttpClientUtil httpClientUtil;

    /*不加(required = false)的话 报无法自动装配。找不到 'DisWxuserMapperWxapp' 类型的 Bean。*/
    @Autowired
    private DisWxuserMapperWxapp disWxuserMapperWxapp;

    private Random random = new Random();

    /**
     * 查询微信用户
     *
     * @param wxId 微信用户主键
     * @return 微信用户
     */
    @Override
    public DisWxuser selectDisWxUserByWxId(Integer wxId) {
        return disWxuserMapperWxapp.selectDisWxuserByWxId(wxId);
    }

    /**
     * 新增微信用户
     *
     * @param disWxuser 微信用户
     * @return 结果
     */
    @Override
    public int insertDisWxuser(DisWxuser disWxuser) {
        return disWxuserMapperWxapp.insertDisWxuser(disWxuser);
    }

    /**
     * 修改微信用户
     *
     * @param disWxuser 微信用户
     * @return 结果
     */
    @Override
    public int updateDisWxuser(DisWxuser disWxuser) throws UnknownHostException {
        disWxuser.modifyWxAvatarurl();
        return disWxuserMapperWxapp.updateDisWxuser(disWxuser);
    }

    /**
     * 查询微信用户
     *
     * @param wxOpenid
     * @return
     */
    @Override
    public DisWxuser selectDisWxuserByWxOpenid(String wxOpenid) {
        return disWxuserMapperWxapp.selectDisWxuserByWxOpenid(wxOpenid);
    }

    /**
     * 设置默认微信用户
     *
     * @param wxOpenid
     * @return
     */

    @Override
    public int setDefaultWxuser(String wxOpenid) throws UnknownHostException {
        DisWxuser disWxuser = new DisWxuser();

        disWxuser.setWxOpenid(wxOpenid);
//        设置默认昵称
        disWxuser.setWxNickname(DisWxuserConstants.WX_USERNAME_LIST[random.nextInt(DisWxuserConstants.WX_USERNAME_LIST.length)]);
//        问题：设置默认头像
        disWxuser.setWxAvatarurl(DisWxuserConstants.WX_URL_LIST[0]);
        disWxuser.modifyWxAvatarurl();
        insertDisWxuser(disWxuser);
        return disWxuser.getWxId();
    }

    @Override
    public String getWxOpenid(String code) {
        String appid = DisWxuserConstants.APPID;
        String secret = DisWxuserConstants.STRING;
//        拼接sql
        String loginUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=" + appid +
                "&secret=" + secret + "&js_code=" + code + "&grant_type=authorization_code";

        String result = httpClientUtil.sendHttpGet(loginUrl);
        if (result != null) {
            JSONObject jsonObject = JSON.parseObject(result);//将json字符串转化为jsonobject
            Object openid = jsonObject.get("openid");
            if (openid != null) {
                return openid.toString();
            }
        }
        return null;
    }
//    泛型构建是从属性字段构建实体类时返回才需要


}
